package prolog.io

import java.io.IOException
import java.io.Writer

import prolog.terms.Int
import prolog.terms.Prog
import prolog.terms.Sink
import prolog.terms.Term


/**
  Writer
*/
object CharWriter extends Sink {
  writer: Writer ;
  
  def this(f: String,p: Prog) ={
    super(p);	
    this.writer=IO.toFileWriter(f);
  }
  
  def this(p: Prog) ={
    super(p);
    this.writer=IO.output;
  }
  
  def putElement(t: Term):int = {
    if(null==writer)
      return 0;
    try {
      c: char=i.asInstanceOf[char];
      writer.write(c);
    } catch {
      case ioe:IOException => return 0
    }
    return 1;
  }
  
  def stop():unit ={
    if(null!=writer&&IO.output!=writer) {
      try {
        writer.close();
      } catch {
	case e:Exception =>
      }
      writer=null;
    }
  }
}